﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32.TaskScheduler;

namespace SQLServerOptimizer.PL.WPF
{
    public class SchedulePerfTask
    {
        public void CreateTask()
        {

            using (TaskService ts = new TaskService())
            {
                 
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "";
                TimeTrigger tt = new TimeTrigger();
                tt.StartBoundary = DateTime.Now.AddMinutes(1); // Default is the time the trigger is instantiated
                // Set the last time the trigger will run to a year from now.
                tt.EndBoundary = DateTime.Now.AddHours(24); // Default is DateTime.MaxValue (or forever)
                // Set the time in between each repetition of the task after it starts to 30 minutes.
                tt.Repetition.Interval = TimeSpan.FromMinutes(1); // Default is TimeSpan.Zero (or never)
                
                td.Triggers.Add(tt);
              
                //// Create an action that will launch Notepad whenever the trigger fires
                td.Actions.Add(new ExecAction(System.Windows.Forms.Application.StartupPath + @"\PlayGround.WPF.exe", "p", null));

                //// Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition(@"SQLServerOptimizerMonitor", td);
               // new TaskService().AddTask("SQLServerScheduledScan", new TimeTrigger(time), new ExecAction(System.Windows.Forms.Application.StartupPath + @"\SQLServerOptimizer.PL.WPF.exe", "p", null));
                //Short Version
                //new TaskService().AddTask("Test", new TimeTrigger(DateTime.Now.AddMinutes(4)), new ExecAction("notepad.exe", "c:\\test.log", null));
               // new TaskService().AddTask("Test", new TimeTrigger(DateTime.Now.AddMinutes(1)), new ExecAction(System.Windows.Forms.Application.StartupPath + @"\SQLServerOptimizer.PL.WPF.exe", "p", null));

            }
        }

        private void removetask(object sender, EventArgs e)
        {
            using (TaskService ts = new TaskService())
            {
                ts.RootFolder.DeleteTask("Test");
            }
        }
    }
}
